package com.atguigu.tingshu.user.api;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

    //api/user/wxLogin/getUserInfo
    @GetMapping("getUserInfo")
    @GuiGuLogin
    public Result<UserInfoVo> getUserInfo(){

        //获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //获取用对象信息
        UserInfo userinfo = userInfoService.getById(userId);
        //拷贝数据
        UserInfoVo userInfoVo = BeanUtil.copyProperties(userinfo, UserInfoVo.class);

        return Result.ok(userInfoVo);
    }

    /**
     * 小程序授权登录
     *  /api/user/wxLogin/wxLogin/{code}
     * @param code
     * @return
     */

    @GetMapping("wxLogin/{code}")
    public Result<Map<String,String>> wxLoginBycode(@PathVariable String code){
       Map<String,String> stringStringMap= userInfoService.wxLoginBycode(code);

        return Result.ok(stringStringMap);
    }

}
